package com.wyh.yuhcloud.util;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @author wyh
 * @version 1.0
 * @date 2021/10/4 17:06
 */
@Component
public class TokenManager {
    //过期时间
    private final long tokenExpires = 60*60*24*7*1000;
    //编码密钥
    private final String tokenSignKey = "1!2@#Q$!EQWw1%";
    //1.根据密钥生成token0
    public String createToken(Map map,long time){
        return Jwts.builder().setClaims(map)
                .setExpiration(new Date(System.currentTimeMillis()+time))
                .signWith(SignatureAlgorithm.HS512,tokenSignKey)
                .compressWith(CompressionCodecs.GZIP).compact();
    }
    //2.根据token得到用户信息
    public String getUserInfoFromToken(String token){
        return (String) Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token).getBody().get("username");
    }
    //根据token得到用户Id
    public String getUserIdFromToken(String token){
        return (String) Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token).getBody().get("userId");
    }
    //3.删除token
    public void removeToken(String token){

    }
}
